package com.dcm.live.service.impl;

import com.dcm.common.constant.Constants;
import com.dcm.common.exception.MyException;
import com.dcm.common.vo.CommonResponse;
import com.dcm.live.entity.LiveRoomEntity;
import com.dcm.live.mapper.LiveRoomMapper;
import com.dcm.live.service.LiveRoomService;
import com.dcm.live.vo.LiveVo;
import com.dcm.live.vo.OnlineVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * [直播间 业务 实现类]
 *
 * @author : [pengrongsheng]
 * @createTime : [2021/3/28]
 */
@Service
public class LiveRoomServiceImpl implements LiveRoomService {

    @Autowired
    private LiveRoomMapper liveRoomMapper;

    @Override
    public List<LiveRoomEntity> findAll() {
        return liveRoomMapper.findAll();
    }

    @Override
    public LiveRoomEntity findRoomById(Long id) {
        return liveRoomMapper.findRoomById(id);
    }

    @Override
    public void addRoom(LiveRoomEntity item) {
        liveRoomMapper.addRoom(item);
    }

    @Override
    public void deleteRoomById(Long id) {
        liveRoomMapper.deleteRoomById(id);
    }

    @Override
    public List<LiveRoomEntity> findRoomByKeyWords(String keyWords) {
        return liveRoomMapper.findRoomByKeyWords(keyWords);
    }

    @Override
    public List<LiveRoomEntity> findRoomByStatus(Integer status) {
        //status 是 0开启 1关闭 二选一
        boolean flag = status == 0 || status == 1;
        if (!flag) {
            //status不是 0 或 1 手动抛出参数校验异常
            throw new MyException(Constants.ResponseMsg.VALIDATE_FAILED);
        }
        return liveRoomMapper.findRoomByStatus(status);
    }

    @Override
    public void updateRoom(LiveRoomEntity liveRoomEntity) {
        if (liveRoomEntity.getId() == null || liveRoomEntity.getId() < 0) {
            throw new MyException(Constants.ResponseMsg.VALIDATE_FAILED);
        }
        liveRoomMapper.updateRoom(liveRoomEntity);
    }

    @Override
    public List<OnlineVo> findOnline(Long uid) {
        LiveRoomEntity room = liveRoomMapper.findRoomByUserId(uid);
        return liveRoomMapper.findOnline(room.getNickName());
    }

    @Override
    public PageInfo<LiveRoomEntity> findPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<LiveRoomEntity> list = liveRoomMapper.findAll();
        PageInfo<LiveRoomEntity> page = new PageInfo<>(list);
        return page;
    }

    /**
     * 查看当前观看人数、当前关注人数、当前点赞人数、当前下单人数
     * @param id
     * @return
     */
    @Override
    public List<LiveVo> findLive(Long id) {
        if (id==null){
            throw new MyException(Constants.LiveErroMsg.LIVE_ID_CANNOT_NULL);
        }
        return liveRoomMapper.findLive(id);
    }

    /**
     * 修改状态
     * @param id
     * @param status
     */
    @Override
    public void liveStatus(Long id, Integer status) {
        if (id==null){
            throw new MyException(Constants.LiveErroMsg.LIVE_ID_CANNOT_NULL);
        }
        if (status==null){
            throw new MyException(Constants.LiveErroMsg.LIVE_STATUS_CANNOT_NULL);
        }
        liveRoomMapper.liveStatus(id,status);
    }

    /**
     *已结束状态进行删除
     * @param id
     */
    @Override
    public void liveDelete(Long id,Integer status){
        if (status==1){
            liveRoomMapper.liveDelete(id,status);
        }else{
            throw new MyException(Constants.LiveErroMsg.LIVE_STATUS_CANNOT_NULL);
        }

    }

    @Override
    public LiveRoomEntity findRoomByUserId(Long likedUserId) {
        return liveRoomMapper.findRoomByUserId(likedUserId);
    }
}
